package com.ljm.sys.dict.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ljm.common.dto.UserInfoDto;
import com.ljm.common.enums.DataStatus;
import com.ljm.common.enums.ResultStatus;
import com.ljm.common.page.PageDto;
import com.ljm.common.util.AssertUtil;
import com.ljm.service.BaseServiceImpl;
import com.ljm.sys.dict.api.SysDictApi;
import com.ljm.sys.dict.dto.SysDictDto;
import com.ljm.sys.dict.entity.SysDict;
import com.ljm.sys.dict.mapper.SysDictMapper;
import com.ljm.sys.dict.service.ISysDictService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
    * 系统字典 服务实现类
    * </p>
 *
 * @author HuamingChen
 * @since 2019-02-14
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class SysDictServiceImpl extends BaseServiceImpl<SysDictMapper, SysDict>
        implements ISysDictService, SysDictApi {

    @Override
    public void save(SysDictDto dto, UserInfoDto userDto){
        SysDict po=new SysDict();
        mapperFacade.map(dto,po);
        if(null==dto.getId()){
            po.setCreateTime(LocalDateTime.now());
            po.setCreater(userDto.getLoginName());
            po.setStatus(DataStatus.NORMAL.getCode());
            po.setOrgId(userDto.getOrgId());
            this.save(po);
        }else{
            po.setEditor(userDto.getLoginName());
            po.setEditTime(LocalDateTime.now());
            this.updateById(po);
        }
    }

    @Override
    public PageDto<SysDictDto> getByPage(String dictKey, String dictValue, int pageNo, int pageSize){
        PageDto<SysDictDto> pageDto=new PageDto<>(pageNo,pageSize);
        IPage<SysDict> page=new Page<>(pageNo,pageSize);
        QueryWrapper<SysDict> wrapper=new QueryWrapper<>();
        if(StringUtils.isNotEmpty(dictKey)){
            wrapper.like("dict_key",dictKey);
        }
        if(StringUtils.isNotEmpty(dictValue)){
            wrapper.like("dict_value",dictValue);
        }
        wrapper.orderByDesc("edit_time");
        page=this.page(page,wrapper);
        if(CollectionUtils.isNotEmpty(page.getRecords())){
            pageDto.setTotal(page.getTotal());
            pageDto.setRecords(mapperFacade.mapAsList(page.getRecords(),SysDictDto.class));
        }
        return pageDto;
    }

    @Override
    public SysDictDto getDataById(Long id){
        if(null==id){
            AssertUtil.throwException(ResultStatus.FIAL.getCode(),"主键为空");
        }
        SysDict po=this.getById(id);
        if(null!=po){
            SysDictDto dto=new SysDictDto();
            mapperFacade.map(po,dto);
            return dto;
        }else{
            return null;
        }
    }

    @Override
    public void del(Long id){
        if(null==id){
            AssertUtil.throwException(ResultStatus.FIAL.getCode(),"主键为空");
        }
        this.removeById(id);
    }

    @Override
    public Map<Class, Class> classMap() {
        Map<Class, Class> map=new HashMap<>();
        map.put(SysDict.class,SysDictDto.class);
        return map;
    }
}
